//
//  SettingsViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 10/26/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "SettingsViewController.h"
#import "Smarthings.h"

@interface SettingsViewController ()

@property (weak, nonatomic) IBOutlet UITableViewCell *baseURLCell;

@end

@implementation SettingsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.baseURLCell.detailTextLabel.text = [Smarthings baseServerURL];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Base URL" message:@"Please insert the new base URL." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
            alert.alertViewStyle = UIAlertViewStylePlainTextInput;
            [alert addButtonWithTitle:@"OK"];
            [alert textFieldAtIndex:0].text = self.baseURLCell.detailTextLabel.text;
            alert.tag = 0;
            [alert show];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 0) {
        if (buttonIndex == 1) {
            self.baseURLCell.detailTextLabel.text = [alertView textFieldAtIndex:0].text;
            [Smarthings setBaseServerURL:self.baseURLCell.detailTextLabel.text];
        }
    }
}

@end
